<?php

namespace app\models;

use Yii;
use yii\helpers\Html;
/**
 * This is the model class for table "{{%project_comment}}".
 *
 * @property int $id
 * @property int $project_id 项目id
 * @property int $userid 评论的用户id
 * @property string $content 评论内容
 * @property int $star 评论星级1,2,3,4,5
 * @property string $picture 评论上传图片
 * @property int $is_top 是否置顶
 * @property int $is_read_by_self 是否仅自己可见
 * @property int $created_time 项目创建时间
 * @property int $updated_time 项目更新时间
 */
class ProjectComment extends \yii\db\ActiveRecord
{
    ////置顶状态
    const CHECK_SET_HOT_0 = 0;//不置顶
    const CHECK_SET_HOT_1 = 1;//置顶

    //仅自己可见状态
    const SEE_STATUS_0 = 0;//所有人可见
    const SEE_STATUS_1 = 1;//自己可见
    
    public static function getSetHotStatus($status=null, $field=null){
         $arr = [
            self::CHECK_SET_HOT_0 => ['id' =>self::CHECK_SET_HOT_0, 'name' => '不置顶', 'class' =>'label-warning btn-xs'],
            self::CHECK_SET_HOT_1 => ['id' =>self::CHECK_SET_HOT_1,'name' => '置顶', 'class' =>'label-success btn-xs'],
        ];
        if (isset($arr[$status][$field])) {
            return $arr[$status][$field];
        }elseif(isset($arr[$status])){
            return Html::tag('span', $arr[$status]['name'], ['class' => 'lable '.$arr[$status]['class']]);
        }

        return array_column($arr, 'name', 'id');
    }

    public static function getSelfSeeStatus($status=null, $field=null){
         $arr = [
            self::SEE_STATUS_0 => ['id' =>self::SEE_STATUS_0, 'name' => '所有人可见', 'class' =>'label-warning btn-xs'],
            self::SEE_STATUS_1 => ['id' =>self::SEE_STATUS_1,'name' => '自己可见', 'class' =>'label-success btn-xs'],
        ];
        if (isset($arr[$status][$field])) {
            return $arr[$status][$field];
        }elseif(isset($arr[$status])){
            return Html::tag('span', $arr[$status]['name'], ['class' => 'lable '.$arr[$status]['class']]);
        }

        return array_column($arr, 'name', 'id');
    }
    
     public function attributes()
    {
        // 添加关联字段到可搜索属性集合
        return array_merge(parent::attributes(), ['username']);
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project_comment}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['is_top', 'is_read_by_self'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'project_id' => Yii::t('app', '项目id'),
            'userid' => Yii::t('app', '评论的用户id'),
            'content' => Yii::t('app', '评论内容'),
            'star' => Yii::t('app', '星级'),
            'picture' => Yii::t('app', '评论图片'),
            'is_top' => Yii::t('app', '置顶状态'),
            'is_read_by_self' => Yii::t('app', '可见状态'),
            'created_time' => Yii::t('app', '评论创建时间'),
            'updated_time' => Yii::t('app', '评论更新时间'),
            'username' => '用户名',
        ];
    }

    //使用图片轮播插件
    public function getPictrues($json_picture){
        $img_arr = json_decode($json_picture, true);
        $items = [];
        if (is_array($img_arr) && !empty($img_arr)) {
            foreach ($img_arr as $key => $value) {
                //大图
                $items['url'] = $value;
                //原图
                $items['src'] = $value;
                //显示标题
                $items['options'] = ['title' => $key];
            }
        }
        return \dosamigos\gallery\Gallery::widget(['items' => $items]);
    }
}
